#-*-coding:utf8;-*-

from ylTDX2 import * #from ylTDX import all def function
from  _utinity import *

# M:=55;
# N:=34;
# LC:=REF(CLOSE,1);
# RSI:=((SMA(MAX((CLOSE - LC),0),3,1) / SMA(ABS((CLOSE - LC)),3,1)) * 100);
# FF:=EMA(CLOSE,3);
# MA15:=EMA(CLOSE,21);
# DRAWTEXT(CROSS(85,RSI),75,'▼'),COLORGREEN;
# VAR1:=IF(YEAR>=2038 AND MONTH>=1,0,1);
# VAR2:=REF(LOW,1)*VAR1;
# VAR3:=SMA(ABS(LOW-VAR2),3,1)/SMA(MAX(LOW-VAR2,0),3,1)*100*VAR1;
# VAR4:=EMA(IF(CLOSE*1.3,VAR3*10,VAR3/10),3)*VAR1;
# VAR5:=LLV(LOW,30)*VAR1;
# VAR6:=HHV(VAR4,30)*VAR1;
# VAR7:=IF(MA(CLOSE,58),1,0)*VAR1;
# VAR8:=EMA(IF(LOW<=VAR5,(VAR4+VAR6*2)/2,0),3)/618*VAR7*VAR1;
# 吸筹:IF(VAR8>100,100,VAR8)*VAR1,COLORRED;
# 庄家吸筹:STICKLINE(吸筹>-150,0,吸筹,8,0),COLORRED;
# 散户: 100*(HHV(HIGH,M)-CLOSE)/(HHV(HIGH,M)-LLV(LOW,M)),COLORFFFF00,LINETHICK2;
# RSV:=(CLOSE-LLV(LOW,N))/(HHV(HIGH,N)-LLV(LOW,N))*100;
# K:=SMA(RSV,3,1);
# D:=SMA(K,3,1);
# J:=3*K-2*D;
# 庄家:EMA(J,6),COLORFF00FF,LINETHICK2;
# 第一买点:DRAWTEXT(CROSS(14,吸筹),10, '买1'),COLORYELLOW;
# 第二买点:DRAWTEXT(CROSS(庄家,散户) AND 庄家<50,30, '买2'),COLORRED;
# 第一卖点:DRAWTEXT(CROSS(88,庄家),80, '卖1'),COLORGREEN;
# 第二卖点:DRAWTEXT(CROSS(散户,庄家),60, '卖2'),COLORCYAN;

def zjxc(code,file_name=None):                     
    df=get_data(code)
    OPEN=tdx(df["Open"].to_list()) #open 是file open
    HIGH=tdx(df["High"].to_list())
    LOW=tdx(df["Low"].to_list())
    CLOSE=tdx(df["Close"].to_list())
    VOL=tdx(df["Volume"].to_list())
    AMOUNT=tdx(df["Amount"].to_list())
    # Today=time.strftime("%Y-%m-%d-%H-%M-%S")
    YEAR=datetime.datetime.today().year
    # MONTH=int(Today.split('-')[1])
    MONTH=datetime.datetime.today().month
    M=54
    N=34 
    LC=REF(CLOSE,1)
    RSI=((SMA(MAX((CLOSE - LC),0),3,1) / SMA(ABS((CLOSE - LC)),3,1)) * 100)
    FF=EMA(CLOSE,3)
    MA15=EMA(CLOSE,21)
    TRI_ANGLE=CROSS(85,RSI) #其实调用RSI.__downcross__(85)
    VAR1=1
    VAR2=REF(LOW,1)*VAR1
    VAR3=SMA(ABS(LOW-VAR2),3,1)/SMA(MAX(LOW-VAR2,0),3,1)*100*VAR1
    VAR4=EMA(IF(CLOSE*1.3,VAR3*10,VAR3/10),3)*VAR1
    VAR5=LLV(LOW,30)*VAR1
    VAR6=HHV(VAR4,30)*VAR1
    VAR7=IF(MA(CLOSE,58),1,0)*VAR1
    VAR8=EMA(IF(LOW<=VAR5,(VAR4+VAR6*2)/2,0),3)/618*VAR7*VAR1
    吸筹=IF(VAR8>100,100,VAR8)*VAR1
    庄家吸筹=IF(吸筹>-150,0,吸筹)
    散户=(HHV(HIGH,M)-CLOSE)/(HHV(HIGH,M)-LLV(LOW,M))*100
    RSV=(CLOSE-LLV(LOW,N))/(HHV(HIGH,N)-LLV(LOW,N))*100
    K=SMA(RSV,3,1)
    D=SMA(K,3,1)
    J=K*3-D*2
    庄家=EMA(J,6)
    第一买点=CROSS(14,吸筹)
    第二买点=CROSS(庄家,散户)
    第一卖点=CROSS(88,庄家)
    第二卖点=CROSS(散户,庄家)
    if file_name is not None:
        output_File = open(file_name,'a+') 
        if len(VAR8)>0:
            output_File.write('{},{},{}\n'.format(str(code).zfill(6),str(round(VAR8[-1],2)),df['close'][-1]))
            output_File.flush()
        else:
            output_File.flush()
        return "%s complete"%str(code).zfill(6)

if __name__=='__main__':
   sort_stratedy(zjxc,ascending=False)
